package com.example.happiness.service.user.Impl;

import com.example.happiness.mapper.UserMapper;
import com.example.happiness.pojo.User;
import com.example.happiness.result.Result;
import com.example.happiness.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public User userLogin(User userid) {
        return userMapper.selectByPrimaryKey(userid.getUserid());
    }

    public Result<User> register(User user) {
        User user1 = userMapper.selectByPrimaryKey(user.getUserid());
        if (user1 != null) {
            return Result.error("手机号已注册");
        }
        user.setCreatetime(LocalDateTime.now());
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapper.insert(user);
        return Result.success();
    }

    public void updataUserInfo(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
